package com.cjhedu.web.controller;

import com.cjhedu.web.domain.Dept;
import com.cjhedu.web.domain.Emp;
import com.cjhedu.web.domain.PageBean;
import com.cjhedu.web.dto.EmpDto;
import com.cjhedu.web.dto.EmpLogDto;
import com.cjhedu.web.service.EmpService;
import com.cjhedu.web.utils.MyResponseResult;
import com.cjhedu.web.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈佳豪
 * @version 1.0
 */
@RestController
//@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("emps")
    public Result getByPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "3") Integer pageSize, EmpDto empDto){
        PageBean<Emp> empPageBean = empService.getByPage(page, pageSize,empDto);
        return Result.success(empPageBean);

    }

    @DeleteMapping("emps/{ids}")
    public Result del(@PathVariable Integer[] ids){
        int del = empService.del(ids);
        return del > 0 ? Result.success() : Result.error("删除失败");
    }

    @PostMapping("emps")
    public Result add(@RequestBody Emp emp){
        int i = empService.addEmp(emp);
        return i > 0 ? Result.success() : Result.error("添加" +emp.getName() + "失败");
    }

    @PutMapping("emps")
    public Result update(@RequestBody Emp emp){
        int update = empService.update(emp);
        return update > 0 ? Result.success() : Result.error("修改失败~~~");
    }

    //修改回显
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }


}
